<template>
  <div class="row">
    <div v-for="column in columnList" :key="column._id" class="col-4 mb-4">
      <div class="card h-100 shadow-sm card-body text-center">
        <img
          :src="column.avatar && column.avatar.url"
          :alt="column.title"
          class="rounded-circle border border-light my-3 m-auto"
        />
        <div class="card-body">
          <h5 class="card-title">{{ column.title }}</h5>
          <p class="card-text text-left">{{ column.description }}</p>
          <router-link
            :to="'/column/' + column._id"
            class="btn btn-outline-primary"
            >进入专栏
          </router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, PropType } from "vue";

import { ColumnProps } from "@/store/index";

export default defineComponent({
  name: "ColumnList",
  props: {
    list: {
      //把构造函数断言成类型，需要使用PropType，传入泛型
      type: Array as PropType<ColumnProps[]>,
      required: true,
    },
  },
  setup(props) {
    // 重新包装columnList数组
    const columnList = computed(() => {
      return props.list.map((column) => {
        if (!column.avatar) {
          //如果avatar图像不存在，就使用默认头像
          column.avatar = {
            url: require("@/assets/column.jpg"),
          };
        }
        return column;
      });
    });
    return {
      columnList,
    };
  },
});
</script>
<style scoped>
.card-body img {
  width: 100px;
  height: 100px;
}
</style>
